import os
import sys

from sanic.config import Config

BASE_DIR = os.path.dirname(os.path.abspath(__name__))
sys.path.append(os.path.join(BASE_DIR, "libs"))


class BaseConfig(Config):
    DEBUG = False   # 调试模式
    PROJECT_NAME = "cognition_bilibili_api"

    DATETIME_FMT = "%Y-%m-%d %H:%M:%S"
    DATE_FMT = "%Y-%m-%d"

    # jwt token
    JWT_SECRET_KEY = "sanic_secret"  # 加密Key
    JWT_TTL = 60 * 60 * 24 * 7  # JWT 7天有效

    SQLALCHEMY_ECHO = True
    # CREATE DATABASE cognition_bilibili DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
    DB_SETTINGS = {
        "dsn": "mysql+aiomysql://root:TingMin1008!@127.0.0.1:3306/cognition_bilibili?charset=utf8mb4",
        "pool_size": 100,
        "pool_recycle": 1200,
        "max_overflow": 10,
    }

    # redis
    REDIS_DSN = os.environ.get("REDIS_DSN", "redis://127.0.0.1:6379/1")

    # http request timeout
    HTTP_TIMEOUT = 5
    ACCESS_LOG = True


class DevelopmentConfig(BaseConfig):
    DEBUG = True
    # 不需要经过中间件的请求
    MIDDLEWARE_REGEX_WHITE_LIST = [
        r"^/swagger"  # swagger 的请求
    ]
    WHITE_LIST = ["swagger"]  # 不需要JWT登录认证的路径
    SIGNATURE_WHITE_LIST = []  # 不需要签名的白名单路径

    # swagger
    # API_HOST = "api.wangge0101.com"  # API的HOST，如果在本地调试，不用加上这个，要不然不会访问 127.0.0.1
    # API_BASEPATH  = "/api"  # API上的BaseURL
    # API_SCHEMES = ["http", "https"]  # API schemes
    # API_VERSION = "1.0.0"   # API版本
    # API_TITLE = "wangge0101 API" # 显示标题
    # API_DESCRIPTION = "API描述"  # 显示描述
    # API_TERMS_OF_SERVICE = "我是API服务团队"  # api 服务团队
    # API_CONTACT_EMAIL = "wangge0101@126.com"    # "我是API联系邮箱"  # 联系邮箱
    # API_LICENSE_NAME = "我是许可证名称"   # 许可证名称
    # API_LICENSE_URL = "https://www.baidu.com/"  # "我是许可证链接URL"  # 许可证 URL
    # API_URI_FILTER = "slash"    # URL是否匹配 "/"
    # SWAGGER_UI_CONFIGURATION = {  # 样式展开
    #     'validatorUrl': None, # Disable Swagger validator
    #     'displayRequestDuration': False,
    #     'docExpansion': 'full'
    # }

    # swagger auth
    # API_SECURITY = [
    #     # {"BasicAuth": []},
    #     # {"ApiKeyAuth": []},
    #     {"TokenAuth": []},
    # ]
    # API_SECURITY_DEFINITIONS = {
    #     # "BasicAuth": {"type": "basic"},
    #     # "ApiKeyAuth": {"type": "apiKey", "in": "header", "name": "X-API-KEY"},
    #     "TokenAuth": {"type": "apiKey", "in": "header", "name": "Authorization"},
    # }

    # Ext
    OAS_IGNORE_HEAD = True  # 是否显示 HEAD 响应函数，默认：True
    OAS_IGNORE_OPTIONS = True   # 是否显示 OPTIONS 响应函数，默认：True
    OAS_PATH_TO_REDOC_HTML = None   # 用于覆盖默认 Redoc HTML 的 HTML 路径，默认：None
    OAS_PATH_TO_SWAGGER_HTML = None     # 用于覆盖默认 Swagger HTML 的 HTML 路径，默认：None
    # 可以设置为 redoc 或 swagger。 控制要在基本路由上显示的 UI。 如果设置为 None，则不会设置文档路由
    OAS_UI_DEFAULT = "swagger"     # 默认："redoc"，和下面 OAS_UI_REDOC OAS_UI_SWAGGER成对使用
    OAS_UI_REDOC = True    # 是否启用 Redoc UI，默认：True
    OAS_UI_SWAGGER = True   # 是否启用 Swagger UI，默认：True

    # OAS_URI_TO_CONFIG = "/openapi-config"   # Swagger 使用的 OpenAPI 配置的 URI 路径，默认："/openapi-config"
    OAS_URI_TO_JSON = "/openapi.json"       # JSON 文档的 URI 路径，默认："/openapi.json"
    # OAS_URI_TO_REDOC = "/redoc"             # Redoc 的 URI 路径，默认："/redoc"
    OAS_URI_TO_SWAGGER = "/swagger"         # Swagger 的 URI 路径，默认："/swagger"
    # OAS_URL_PREFIX = "/docs"                # 用于 OpenAPI 文档蓝图的 URL 前缀，默认："/docs"



class TestingConfig(BaseConfig):
    pass


class ProductionConfig(BaseConfig):
    ACCESS_LOG = False
    SQLALCHEMY_ECHO = False


config = {
    'development': DevelopmentConfig(),   # 开发
    'testing': TestingConfig(),           # 测试
    'production': ProductionConfig(),     # 生产
    'default': DevelopmentConfig(),       # 默认开发
}


# TODO *** 运行环境 ***
ENV = "development"


def get_config():
    return config[ENV]
